<template>
  <div class="layout-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
        <div class="logo">
          <h3 v-if="!isCollapse">文华学院</h3>
          <h3 v-else>文</h3>
        </div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical"
          :collapse="isCollapse"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/dashboard">
            <el-icon><House /></el-icon>
            <template #title>首页</template>
          </el-menu-item>
          <el-menu-item index="/students">
            <el-icon><User /></el-icon>
            <template #title>学生管理</template>
          </el-menu-item>
          <el-menu-item index="/courses">
            <el-icon><Reading /></el-icon>
            <template #title>课程管理</template>
          </el-menu-item>
          <el-menu-item index="/enrollments">
            <el-icon><Document /></el-icon>
            <template #title>选课管理</template>
          </el-menu-item>
          <el-menu-item index="/student-credits" v-if="userRole === 0 || userRole === 1">
            <el-icon><DataAnalysis /></el-icon>
            <template #title>学分统计</template>
          </el-menu-item>
          <el-menu-item index="/ai-query">
            <el-icon><Search /></el-icon>
            <template #title>智能查询</template>
          </el-menu-item>
          <el-menu-item index="/users" v-if="userRole === 0 && DEBUG_MODE">
            <el-icon><UserFilled /></el-icon>
            <template #title>用户管理</template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 头部 -->
        <el-header class="header">
          <div class="header-left">
            <el-button
              type="text"
              @click="toggleCollapse"
              class="collapse-btn"
            >
              <el-icon><Expand v-if="isCollapse" /><Fold v-else /></el-icon>
            </el-button>
            <el-breadcrumb separator="/" class="breadcrumb">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item v-if="$route.meta.title">{{ $route.meta.title }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-info">
                <el-icon><Avatar /></el-icon>
                {{ username }}
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 主内容区 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import {
  House,
  User,
  Reading,
  Document,
  UserFilled,
  DataAnalysis,
  Search,
  Expand,
  Fold,
  Avatar,
  ArrowDown
} from '@element-plus/icons-vue'
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'

export default {
  name: 'Layout',
  components: {
    House,
    User,
    Reading,
    Document,
    UserFilled,
    DataAnalysis,
    Search,
    Expand,
    Fold,
    Avatar,
    ArrowDown
  },
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const isCollapse = ref(false)
    
    // 调试开关 - 设置为true时显示调试日志
    const DEBUG_MODE = true
    
    // 使用Pinia状态管理获取用户信息
    const username = computed(() => userStore.userInfo.hm || '用户')
    const userRole = computed(() => {
      const role = userStore.userInfo?.roleValue ?? 2
      if (DEBUG_MODE) {
        console.log('=== 菜单权限调试信息 ===')
        console.log('完整用户信息:', userStore.userInfo)
        console.log('role字段值:', userStore.userInfo?.role)
        console.log('roleValue字段值:', userStore.userInfo?.roleValue)
        console.log('roleValue是否为undefined:', userStore.userInfo?.roleValue === undefined)
        console.log('roleValue是否为0:', userStore.userInfo?.roleValue === 0)
        console.log('计算得到的userRole:', role)
        console.log('userRole类型:', typeof role)
        console.log('菜单条件判断结果 (role === 0 || role === 1):', role === 0 || role === 1)
        console.log('========================')
      }
      return role
    })

    const toggleCollapse = () => {
      isCollapse.value = !isCollapse.value
    }

    const handleCommand = async (command) => {
      if (command === 'profile') {
        router.push('/profile')
      } else if (command === 'logout') {
        ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await userStore.logout()
          ElMessage.success('退出登录成功')
        })
      }
    }

    return {
      isCollapse,
      username,
      userRole,
      toggleCollapse,
      handleCommand
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  transition: width 0.3s;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b3a4b;
  color: white;
  font-size: 18px;
  font-weight: bold;
}

.el-menu-vertical {
  border-right: none;
  height: calc(100vh - 60px);
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  margin-right: 20px;
  font-size: 18px;
}

.breadcrumb {
  font-size: 14px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
}

.user-info .el-icon {
  margin: 0 5px;
}

.main-content {
  background-color: #f5f5f5;
  padding: 20px;
}
</style>